<template>
    <div class="content-page">
        <div class="content-nav">
            <el-breadcrumb class="breadcrumb" separator="/">
                <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>商品管理</el-breadcrumb-item>
                <el-breadcrumb-item>商品属性</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="operation-nav">
                <router-link to="/dashboard/attribute/add">
                    <el-button type="primary" icon="plus">添加商品属性</el-button>
                </router-link>
            </div>
        </div>
        <div class="content-main">
            <div class="filter-box">
                <el-form :inline="true" :model="filterForm" class="demo-form-inline">
                    <el-form-item label="属性名称">
                        <el-input v-model="filterForm.name" placeholder="属性名称"></el-input>
                    </el-form-item>
                    <el-form-item label="属性分类" prop="category_id_parent">
                        <el-select v-model="filterForm.attribute_category_id" placeholder="请选择属性分类">
                            <el-option v-for="item in attributecategory" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmitFilter">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="form-table-box">
                <el-table :data="tableData" style="width: 100%" border stripe>
                    <el-table-column prop="id" label="ID" width="100">
                    </el-table-column>
                    <el-table-column prop="attribute_category_id" label="属性分类编号">
                    </el-table-column>
                    <el-table-column prop="name" label="属性名称">
                    </el-table-column>
                    <el-table-column prop="sort_order" label="排序">
                    </el-table-column>
                    <el-table-column label="操作" width="140">
                        <template scope="scope">
                            <el-button size="small" @click="handleRowEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="small" type="danger" @click="handleRowDelete(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="page-box">
                <el-pagination @current-change="handlePageChange" :current-page="page" :page-size="10" layout="total, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>

    export default {
        name: "AttributePage",
        data() {
            return {
                page: 1,
                total: 0,
                filterForm: {
                    name: '',
                    attribute_category_id:0,

                },
                tableData: [],
                attributecategory: [
                    {
                        id: 0,
                        name: '全部'
                    }
                ],
            }
        },

        methods: {
            handlePageChange(val) {
                this.page = val;
                //保存到localStorage
                localStorage.setItem('attributePage', this.page)
                localStorage.setItem('attributeFilterForm', JSON.stringify(this.filterForm));
                this.getList()
            },
            handleRowEdit(index, row) {
                this.$router.push({ name: 'attribute_add', query: { id: row.id } })
            },
            handleRowDelete(index, row) {

                this.$confirm('确定要删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    this.axios.post('attribute/destory', { id: row.id }).then((response) => {
                        console.log(response.data)
                        if (response.data.errno === 0) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });

                            this.getList();
                        }
                    })


                });
            },
            onSubmitFilter() {
                this.page = 1
                this.getList()
            },
            getList() {
                this.axios.get('attribute', {
                    params: {
                        page: this.page,
                        name:this.filterForm.name,
                        attribute_category_id: this.filterForm.attribute_category_id
                    }
                }).then((response) => {
                    this.tableData = response.data.data.data
                    this.page = response.data.data.currentPage
                    this.total = response.data.data.count
                })
            },
            getAttributeCategory() {
                this.axios.get('attributecategory/list').then((response) => {
                    this.attributecategory = this.attributecategory.concat(response.data.data);
                })
            },
        },
        components: {

        },
        mounted() {
            this.getList();
            this.getAttributeCategory();
        }
    }

</script>

<style scoped>

</style>

